<?php
/**
* @version $Id: controller.php 18 2009-12-07 11:22:56Z truongnhan0311 $
* @author truongnhan0311
* @package	wrd component
*/
// no direct access
defined( '_JEXEC' ) or die( 'Restricted access' );
jimport( 'joomla.application.component.view');
require_once JPATH_BASE.DS.'administrator'.DS.'components'.DS.'com_wrd'.DS.'classes'.DS.'utilities'.DS.'pointProvider.php';


class WrdViewProfile extends WrdView {
	
	protected $pagination;
	
	function display($tpl = null) {
		$user = JFactory::getUser();
		if($user->id != 0){
			
			$user->profile = WrdHelper::getUserProfile($user->id,'userid');
			
			$profgile_key = array();
			$profgile_key['member_id']			= 'Mã thành viên';
			$profgile_key['prefix']				= 'Danh xưng';
			$profgile_key['dob']				= 'Ngày tháng năm sinh';
			$profgile_key['f_name']				= 'Tên';
			$profgile_key['l_name']				= 'Họ';
			$profgile_key['name_on_card']		= 'Tên hiển thị trên thẻ';
			$profgile_key['nationality']		= 'Quốc tịch';
			$profgile_key['p_email']			= 'Email liên lạc';
			$profgile_key['alternative_email']	= 'Email cá nhân';
			$profgile_key['country_code']		= 'Mã di động Quốc Tế';
			$profgile_key['phone']				= 'Điện thoại di động';
			$profgile_key['address']			= 'Địa chỉ';
			$profgile_key['country']			= 'Quốc gia';
			$profgile_key['city']				= 'Thành phố';
			$profgile_key['distric']			= 'Quận';
			$profgile_key['pick_up_at']			= 'Nơi nhận thẻ VIP';
			$profgile_key['lprefer']			= 'Ngôn ngữ thể hiện';
			$profgile_key['referer']			= 'Email của người giới thiệu';
			$profgile_key['referer_phone']		= 'Số điện thoại người giới thiệu';
			$profgile_key['status']				= 'Tình Trạng Hôn Nhân';
			$profgile_key['website']			= 'Website';
			$profgile_key['skype']				= 'Skype';
			$profgile_key['yahoo']				= 'Yahoo';
			$profgile_key['gtalk']				= 'Gtalk';
			$profgile_key['msn']				= 'MSN';
			$profgile_key['facebook']			= 'Facebook';
			$profgile_key['hobbies']			= 'Sở Thích';
			$profgile_key['other_hobbies']		= 'Sở thích khác';
			$profgile_key['own_car']			= 'Bạn có sở hữu xe hơi không ?';
			$profgile_key['num_children']		= 'Bạn có bao nhiêu người con ?';
			$profgile_key['weekend_action']		= 'Bạn Thích Làm Gì Vào Cuối Tuần';
			$profgile_key['o_a_weekend']		= 'Sở thích khác vào cuối tuần';
			$profgile_key['job_prefix']			= 'Chức vụ';
			$profgile_key['o_job_prefix']		= 'Chức vụ khác';
			$profgile_key['job']				= 'Công Việc';
			$profgile_key['o_job']				= 'Công việc khác';
			$profgile_key['department']			= 'Bộ Phận';
			$profgile_key['o_department']		= 'Bộ Phận Khác';
			$profgile_key['company_name']		= 'Tên Công ty';
			$profgile_key['industry']			= 'Lĩnh vực';
			$profgile_key['o_industry']			= 'Lĩnh vực Khác';
			$profgile_key['c_info']				= 'Web công ty';
			$profgile_key['c_addr']				= 'Địa chỉ công ty';
			$profgile_key['c_country']			= 'Công ty thuộc Quốc gia';
			$profgile_key['c_city']				= 'Công ty thuộc thành phố';
			$profgile_key['c_distric']			= 'Công ty thuộc quận';
			$profgile_key['education']			= 'Trình độ học vấn';
			$profgile_key['o_education']		= 'Trình độ học vấn khác';
			$profgile_key['o_club']				= 'Thành viên của các câu lạc bộ khác';
			$profgile_key['o_o_clubs']			= 'Các Câu Lạc Bộ Khác';
			$profgile_key['income']				= 'Mức thu nhập';
			$profgile_key['bation']				= 'Tổ chức doanh nghiệp';
			$profgile_key['o_bation']			= 'Hiệp Hội Doanh Nghiệp Khác';
			$user->profgile_key					= $profgile_key;
			
			$profgile_key_dropdown[] = 'nationality';
			$profgile_key_dropdown[] = 'country';
			$profgile_key_dropdown[] = 'city';
			$profgile_key_dropdown[] = 'distric';
			$profgile_key_dropdown[] = 'pick_up_at';
			$profgile_key_dropdown[] = 'lprefer';
			$user->profgile_key_dropdown		= $profgile_key_dropdown;
		}

		$fnc = 'display'.ucfirst($this->getLayout());
		if (is_callable(array(&$this, $fnc))){
			$this->$fnc($tpl);
		}else{
			parent::display($tpl);
		}
		
		
	}
	
    function displayUserdetail($tpl = null) {
        
        $mainframe = &JFactory::getApplication();
        $user = JFactory::getUser();
        $id = JRequest::getInt('id');
        
        if($user->id != 0){
            $model_profile = $this->getModel('profile');
            $user_info = $model_profile->getFullUserProfile($user->id);
        }
        
        $this->assignRef('model', $user_info);
        $this->assignRef('user', $user);
        parent::display($tpl);
        
    }
    
	#id =  current step
	function displayDefault($tpl = null){
		
		$mainframe = &JFactory::getApplication();
		$user = &JFactory::getUser();
		
		$id = JRequest::getInt('id');
        
		$configField = WrdHelper::getConfigFieldRegistration();
		
		if($user->id != 0){
			$model_profile = $this->getModel('profile');
			$user_info = $model_profile->getUserProfile($user->id);
			$user_totalstepreg = $model_profile->getTotalStepReg();
			$user_info->total_step = $user_totalstepreg;
			$step = $user_info->step;
			
			if($mainframe->getUserState('profile.registration.value') == 0){
				if(($step == 5 && $id==0) || ($id == 0 && $step > 1)){
					$mainframe->redirect(WrdHelperRoute::getProfileFinishRoute(),'My Profile');
					return true;
				}
			}
			
			$mainframe->setUserState('profile.registration.value', 0);
			
			if($id <= $step && $id != 0)
				$step = $id;
			
			//exit();
			switch ($step){
				case 1:
					$this->registration($user_info);
					break;
				case 2:
					$this->personal($user_info);
					break;
				case 3:
					$this->commercial($user_info);
					break;
				case 4:
					$this->professionalism($user_info);
					break;
				default:
					$mainframe->redirect(WrdHelperRoute::getProfileFinishRoute(),'My Profile');
					break;
			}
			
			$p_o_u = WrdHelper::getPoints($user->id);
			$this->assignRef('p_o_u', $p_o_u);
			$pointstep = WrdHelper::machPoint($step,'registration');
			$this->assignRef('pointstep', $pointstep->point);
			
		}
		else{
			$mainframe->redirect('index.php#login');
			//$this->registration($user_info);
		}
		
		$this->assignRef('configField', $configField);
		$this->assignRef('cstep', $step);
		$this->assignRef('user_info', $user_info);
		$this->assignRef('user', $user);
		parent::display($tpl);
	}
	
	function displayFinish($tpl = null){
		$mainframe = &JFactory::getApplication();
		$mainframe->redirect(JRoute::_('index.php?option=com_wrd&view=profile&layout=default&id=1:my-account&lang=vi'));
		//exit();
		$user = JFactory::getUser();
		$document =& JFactory::getDocument();
		$document->addScript(JURI::base() . 'components/com_wrd/assets/js/wrd.js');
		if($user->id == 0){
			$mainframe->redirect(WrdHelperRoute::getProfileLoginRoute(),'You must login first');
		}
        
        $model_profile = $this->getModel('profile');
        $user_info = $model_profile->getUserProfile($user->id);        
        $step = $user_info->step;
        
        if($step == 1) {
            $mainframe->redirect(WrdHelperRoute::getProfileRoute());    
        }
            
		$a_mmonth = JRequest::getVar('month') ;
		$a_year   =  JRequest::getVar('year') ;
		
		$lists['month'] = JHTML::_('wrd.genarateListBox','month',$a_mmonth,'month',1,12,'select' );
		$lists['year'] = JHTML::_('wrd.genarateListBox','year',$a_year,'year',2007, date('Y'),'select' );
		
		//profiledetail
		$profile = WrdHelper::getUserProfile($user->id);
		
        //get transaction history
		$transaction = WrdHelper::getTransaction ($profile->member_id, $a_mmonth, $a_year );
		$p_o_tran = array() ;
		$model_point = $this->getModel('exchange')	;
		if(!empty($transaction)){
			foreach ($transaction as $tran){
				$tt = 	$model_point->getPointOfTran ($profile->user_id, $tran->transaction_id ) ;
				$p_o_tran[$tran->id] = $tt->point ;
			}
		}
		$p_title = WrdHelper::getPageTitle() ;
		
		$p_o_u = WrdHelper::getPoints($user->id);
		$this->assignRef('profile', $profile);
		$this->assignRef('p_o_u', $p_o_u);
		$this->assignRef('transaction', $transaction);
		$this->assignRef('p_title', $p_title );
		$this->assignRef('lists',	$lists);
		$this->assignRef('p_o_tran',	$p_o_tran);
		
		parent::display($tpl);
	}
	
	function displayLogin($tpl = null){
		
		$mainframe = &JFactory::getApplication();
		$configField = WrdHelper::getConfigFieldRegistration();
		
		$user = JFactory::getUser();
		$user_info = array();
		
		if($user->id != 0){
			$mainframe->redirect(WrdHelperRoute::getProfileRoute(),'You must login first');
		}
		else{
			$this->assignRef('configField', $configField);
			parent::display($tpl);
		}
	}
	
    /**
     * My Account
     */    
    function displayMyaccount($tpl){
    	$post = JRequest::get('post');
    	
    	if($post['point_from_date'] != '' && $post['point_to_date'] != '' && $post['special_range'] == 0){
    		//get from other
    		$pfd = strtotime($post['point_from_date']);
    		$ptd = strtotime($post['point_to_date']);
    		$post['special_range'] = 0;
    		
    		if($pfd >= $ptd){
    			$begin = date('Y-m-d',$ptd).' 00:00:00';
    			$end = date('Y-m-d',$pfd).' 23:59:59';
    			
    			$cbegin = date('d-m-Y',$ptd);
    			$cend = date('d-m-Y',$pfd);
    			
    			$this->assignRef('point_from_date', $cbegin);
    			$this->assignRef('point_to_date', $cend);
    			
    			$this->assignRef('special_range', $post['special_range']);
    		}
    		else{
    			$begin = date('Y-m-d',$pfd).' 23:59:59';
    			$end = date('Y-m-d',$ptd).' 23:59:59';
    			
    			$cbegin = date('d-m-Y',$pfd);
    			$cend = date('d-m-Y',$ptd);
    			
    			$this->assignRef('point_from_date', $cbegin);
    			$this->assignRef('point_to_date', $cend);
    			
    			$this->assignRef('special_range', $post['special_range']);
    		}
    	}else{
    		$cbegin = '';
    		$cend = '';
    		$this->assignRef('point_from_date', $cbegin);
    		$this->assignRef('point_to_date', $cend);
    		
    		switch ($post['special_range']) {
    			case 1:
    					$begin = date('Y-m-01').' 00:00:00';
    					$end = date('Y-m-t').' 23:59:59';
    					$this->assignRef('special_range', $post['special_range']);
    				break;
    			case 2:
    					$begin  = date('Y-m-d', strtotime('first day of last month')).' 00:00:00';
    					$end  = date('Y-m-d', strtotime('last day of last month')).' 23:59:59';
    					$this->assignRef('special_range', $post['special_range']);
    				break;
    			case 3:
    					$end  = date('Y-m-d').' 23:59:59';
    					$begin = date('Y-m-d', strtotime('first day of -3 month')).' 00:00:00';
    					$this->assignRef('special_range', $post['special_range']);
    				break;
    			default:
    				$end  = date('Y-m-d').' 23:59:59';
    				$begin = date('Y-m-d', strtotime('first day of -3 month')).' 00:00:00';
    				$post['special_range'] = 3;
    				$this->assignRef('special_range', $post['special_range']);
    				break;
    		}
    	}
    	
    	
        $db = &JFactory::getDbo();
        $mainframe = &JFactory::getApplication();
        $lang = &JFactory::getLanguage()->getTag();
        $user = &JFactory::getUser();

        /*** Check if user login ***/
        if($user->id <= 0){
             $mainframe->redirect(('index.php#login'));
        }
        
        $profile = WrdHelper::getUserProfile($user->id);
        $modelProfile = $this->getModel('profile');
        $profile = $modelProfile->getUserProfile($user->id);
        
        $level1 = $this->getPointSumaryLevel1($user->id, $profile->member_id,$begin,$end);
        
        $this->assignRef('level1', $level1);
        $this->assignRef('profile', $profile);
        parent::display($tpl);
    }
    
    /**
     * My Point
     */    
    function displayMypoint(){
		
		$startRow = JRequest::getVar('limitstart');
		if($startRow == NULL){
            $startRow = 0;
        }
        $db = JFactory::getDbo();
        $mainframe = &JFactory::getApplication();
        $lang = JFactory::getLanguage()->getTag();
        $user = JFactory::getUser();
        $query = "SELECT count(*) FROM #__wrd_point WHERE user_id =".$user->id;
        $db->setQuery($query);
        $total = $db->loadColumn();
        
        $pagination = & $this->get('Pagination');
        $this->_pagination = new JPagination($total[0],$startRow,10);
    
		$this->assignRef('pagination', $this->_pagination);

        if($user->id <= 0){
            
             $mainframe->redirect(('index.php?option=com_wrd&view=profile&layout=login&Itemid=120'));
        }
        $points = PointProvider::getPointOfUserFE($db, array('userId'=>$user->id),$startRow);

        $this->assignRef('points', $points);    
        parent::display();
    }
    
	function displayReset(){
		$configField = WrdHelper::getConfigFieldRegistration();
		$this->assignRef('configField', $configField);
		parent::display();
	}
	
	function displayConfirm(){
		$configField = WrdHelper::getConfigFieldRegistration();
		$this->assignRef('configField', $configField);
		parent::display();
	}
	
	function displayComplete(){
		$configField = WrdHelper::getConfigFieldRegistration();
		$this->assignRef('configField', $configField);
		parent::display();
	}
	
	function account($detail){
		parent::display();
	}
	
    function displayPromotion($tpl = null){
        
        $mainframe = JFactory::getApplication();
        $user = JFactory::getUser();
        self::checkIfUserNotDoneStepOne($mainframe, $user);
        $lists = array();
        $p_o_tran = array();
        
        $this->assignRef('lists',    $lists);
        $this->assignRef('p_o_tran',    $p_o_tran);
        
        parent::display($tpl);
    }
    
    private function checkIfUserNotDoneStepOne($mainframe, $user){
        
        if($user->id == 0){
            $mainframe->redirect(WrdHelperRoute::getProfileLoginRoute(),'You must login first');
        }
        
        $model_profile = $this->getModel('profile');
        $user_info = $model_profile->getUserProfile($user->id);        
        $step = $user_info->step;
        
        if($step == 1) {
            $mainframe->redirect(WrdHelperRoute::getProfileRoute());    
        }
    }
    
	function registration($detail){
		
		
		$mainframe = &JFactory::getApplication();
		$context = JRequest::getCmd('option');
		$view = JRequest::getCmd('view');
		
		if(empty($detail->nationality)){
			$detail->nationality = '0' ;
		}
		$lists = array() ;
		$lists['prefix'] 			= JHTML::_('wrd.prefix',$detail->prefix,'',1, 'width60 form-control');
		$lists['nationality'] 			= JHTML::_('wrd.listCountry','nationality',$detail->nationality,'','width305 form-control',1 );
		$filter_country_id 			= $mainframe->getUserStateFromRequest($context.$view.'country','country',$detail->country,'int');
		
		if(empty($filter_country_id)){
			$filter_country_id = '0' ;
		}
		$javascript_country_to_city = "disabled onchange=\"changeCity( 'city', sectioncountrys,document.default_registration_form.country.options[document.default_registration_form.country.selectedIndex].value , 0, 0);\"";
		$filter_city_id 			= $mainframe->getUserStateFromRequest($context.$view.'city','city',$detail->city,'int');
		$javascript_city_to_distric = "onchange=\"changeDistric( 'distric', sectioncitys,document.default_registration_form.city.options[document.default_registration_form.city.selectedIndex].value , 0, 0);\"";
		
		$lists['country'] 			= JHTML::_('wrd.listCountry','country',$filter_country_id,$javascript_country_to_city,'width305 form-control',1 );
		$lists['city'] 				= JHTML::_('wrd.listCity','city',$filter_city_id,$javascript_city_to_distric,'width130 form-control',1 );
		$lists['distric'] 			= JHTML::_('wrd.listDistric','distric',$detail->distric,'','width145 form-control' );
		$lists['countryToCity'] 	= JHTML::_('wrd.countryToCity');
		$lists['cityToDistric'] 	= JHTML::_('wrd.cityToDistric');		
		$lists['pick_up_oulet'] 	= JHTML::_('wrd.listOutletsP','pick_up_at',$detail->pick_up_at, '' , 'width305 form-control' , '' , $condition = 'and pid != 00 and published_cpoint = 1');
		$lists['o_source'] 	= JHTML::_('wrd.listOutletsP','o_source',$detail->o_source, '' , 'width305 form-control' , '' , $condition = 'and pid != 00');
		
		$user_birth = explode('-', $detail->dob) ;
		if(intval($user_birth[2] == 0))
			$lists['day_birth'] 		= JHTML::_('wrd.listDateOfMonth','day_birth',$user_birth[2],'','width70 form-control');
		else {
			$lists['day_birth'] = $user_birth[2].'-';
			$detail->dob_already = 1;
		}
		
		if(intval($user_birth[1] == 0))
			$lists['month_birth'] 		= JHTML::_('wrd.listMonthOfYear','month_birth',$user_birth[1],'','width70 form-control');
		else{ 
			$lists['month_birth'] = $user_birth[1].'-';
			$detail->dob_already = 1;
		}
								
		if(intval($user_birth[0] == 0))
			$lists['year_birth'] 		= JHTML::_('wrd.listYear','year_birth',$user_birth[0],'','width70 form-control');
		else {
			$lists['year_birth'] = $user_birth[0];
			$detail->dob_already = 1;
		}
		
		$lists['lang_prefer'] 		= JHTML::_('wrd.lprefer','lprefer',$detail->lprefer,'','lprefer width305 form-control',0);
		
		
		$this->assignRef('lists',	$lists);
		$this->assignRef('data',	$detail);
		return true;
	}
	
	function personal($detail){
		$model_hobbies = JModelLegacy::getInstance('hobbies','WrdModel');
		
		$lists['status'] 	= JHTML::_('wrd.listStatus','status',$detail->status ,'','form-control');
		$lists['numChild'] 	= JHTML::_('wrd.listNumChildren','num_children',$detail->num_children ,'','width318 form-control');
		$this->assignRef('lists',	$lists);
		$this->assignRef('data',	$detail);
		
        $social = unserialize($detail->social);
		$this->assignRef('social',	$social);
		
		$all_hobbies = $model_hobbies->getListHobbies();
		$list_hobbies = $model_hobbies->getParentItem();
		$child_hob = array();
		$hobbi_name = array();
		foreach($all_hobbies as $parent){
			$child_hob[$parent->id] =  $model_hobbies->getItemChild($parent->id);
			$hobbi_name[$parent->id] =  $model_hobbies->getItemName($parent->id);
		}
		
        $model_act_weekend = JModelLegacy::getInstance('aweekend','WrdModel');
		$list_aweekend = $model_act_weekend->getList();
        $this->assignRef('list_aweekend',	$list_aweekend);
		$u_act_w = unserialize($detail->weekend_action);
		$this->assignRef('u_act_w',	$u_act_w);
        
		$this->assignRef('hobbi_name',	$hobbi_name);
		$this->assignRef('all_hobbies',	$all_hobbies);
		$this->assignRef('list_hobbies',	$list_hobbies);
		$this->assignRef('list_child_hobbies',	$child_hob);
		$icons_path = "components/com_wrd/assets/icons/";
		$this->assignRef('icons_path',	$icons_path);
		$u_hobbies = unserialize($detail->hobbies);
		$this->assignRef('u_hobbies',	$u_hobbies);
		
		return true;
	}
	
	function commercial($detail){
		$mainframe = &JFactory::getApplication();
		$context = JRequest::getCmd('option');
		$view = JRequest::getCmd('view');
		$user = JFactory::getUser();
		
		$js_job_prefix				= "onchange=\"showOther( 'job_prefix','fl_job_prefix');\"";
		$lists['job_prefix'] 		= JHTML::_('wrd.job_prefix',$detail->job_prefix ,$js_job_prefix,0,'width60 form-control');
		
		$js_job 					= "onchange=\"showOther( 'job','fl_o_job');\"";
		$lists['job'] 				= JHTML::_('wrd.listJob','job',$detail->job ,$js_job,'job form-control','e');
		
		$js_industry 				= "onchange=\"showOther( 'industry','fl_o_industry');\"";
		$lists['industry'] 			= JHTML::_('wrd.listIndustry','industry',$detail->industry ,$js_industry,'form-control');
		
		$js_department 				= "onchange=\"showOther( 'department','fl_o_department');\"";
		$lists['department'] 		= JHTML::_('wrd.listDepartment','department',$detail->department ,$js_department,'c_country form-control');
		
		if(!empty($detail->c_country))
			$detail->c_country = '249' ;
		
		$filter_country_id 			= $mainframe->getUserStateFromRequest($context.$view.'c_country','c_country',$detail->c_country,'int');
		$javascript_country_to_city = "onchange=\"changeCity( 'c_city', sectioncountrys,document.default_registration_form.c_country.options[document.default_registration_form.c_country.selectedIndex].value , 0, 0);\"";
		
		$filter_city_id 			= $mainframe->getUserStateFromRequest($context.$view.'c_city','c_city',$detail->c_city,'int');
		$javascript_city_to_distric = "onchange=\"changeDistric( 'c_distric', sectioncitys,document.default_registration_form.c_city.options[document.default_registration_form.c_city.selectedIndex].value , 0, 0);\"";
		
		$lists['country'] 			= JHTML::_('wrd.listCountry','c_country',$filter_country_id,$javascript_country_to_city,'c_country form-control' );
		$lists['city'] 				= JHTML::_('wrd.listCity','c_city',$filter_city_id,$javascript_city_to_distric,'city form-control' );
		$lists['distric'] 			= JHTML::_('wrd.listDistric','c_distric',$detail->c_distric,'','distric form-control');
		
		$lists['countryToCity'] 	= JHTML::_('wrd.countryToCity');
		$lists['cityToDistric'] 	= JHTML::_('wrd.cityToDistric');		

		$this->assignRef('lists',	$lists);
		$this->assignRef('data',	$detail);
		return true;
	}
	
	function professionalism($detail){
		$js_c_education = "onchange=\"showOther( 'education','fl_o_education');\"";
		$lists['education'] 	= JHTML::_('wrd.listEducation','education',$detail->education ,$js_c_education,'form-control',5);
		$js = "onchange=\"showOther( 'o_club','fl_o_club');\"";
		$o_club = unserialize($detail->o_club);
		$lists['o_club'] 		= JHTML::_('wrd.listOClub','o_club[]',$o_club ,'',' form-control',5);
		$lists['income'] 		= JHTML::_('wrd.listIncome','income',$detail->income ,'','form-control');
		$js_c_bation = "onchange=\"showOther( 'bation','fl_o_bation');\"";
		$bations = unserialize($detail->bation);
		$lists['bation'] 		= JHTML::_('wrd.listBation','bation[]',$bations ,'',' form-control ',5);
		$this->assignRef('lists',	$lists);
		$this->assignRef('data',	$detail);
		return true;
		
	}

	function displayThanks($tpl){
		$thankspage = WrdHelper::getThanksPage();
		$this->assignRef('thankspage', $thankspage->content);
		parent::display($tpl);
	}
	
	function getPointSumaryLevel1($user_id, $vip_id,$begin,$end){
		$db = &JFactory::getDbo();
		$user = &JFactory::getUser();
		
		$query = 'SELECT type, created_date,transaction,redemption_id,count(id) as total,point,expire
				FROM #__wrd_point 
				WHERE user_id = '.$user->id.' 
				AND created_date BETWEEN "'.$begin.'" AND "'.$end.'"
				GROUP BY type,DATE(created_date),redemption_id 
				ORDER BY created_date DESC' ;
		//echo str_ireplace('#_', 'vip', $query);
		$rs = $db->setQuery($query)->loadObjectList();
		
		foreach ($rs as $row=>$item){
			if($item->type == 0 && $item->total == 1){
				//get transaction name
				$sub_query = 'SELECT b.`name` 
								FROM #__wrd_transaction as a 
								LEFT JOIN #__wrd_outlets as b on a.outlet = b.`code` 
								WHERE transaction_id = '.$item->transaction;
				
				$outlet = $db->setQuery($sub_query)->loadObject();
				$rs[$row]->des =  $outlet->name;
				
				$rs[$row]->sub_level2 = $this->getPointSumaryLevel2($item->type, $user_id, $vip_id, $item->created_date);
				
				
			}else if($item->type == 0 && $item->total > 1){
				$rs[$row]->des =  JText::_('WRD_MYACCOUNT_TRANSACTION_IN_DAY');
				$rs[$row]->sub_level2 = $this->getPointSumaryLevel2($item->type, $user_id, $vip_id, $item->created_date);
			}
			else if($item->type == 1){
				$rs[$row]->des = JText::_('WRD_MYACCOUNT_PROFILE');
				$rs[$row]->sub_level2 = $this->getPointSumaryLevel2($item->type, $user_id, $vip_id, $item->created_date);
			}
			else if($item->type == 2){
				$rs[$row]->des =  JText::_('WRD_MYACCOUNT_ADJUST_POINT');
				$rs[$row]->sub_level2 = $this->getPointSumaryLevel2($item->type, $user_id, $vip_id, $item->created_date);
			}
			else if($item->type == 3){
				$rs[$row]->des =  JText::_('WRD_MYACCOUNT_BANNER_CLICK');
			}
			else if($item->type == 4){
				$rs[$row]->des =  JText::_('WRD_MYACCOUNT_PROMOTION_CLICK');
			}
			else if($item->type == 9){
				$rs[$row]->des =  JText::_('WRD_MYACCOUNT_REDEMTION');
				$rs[$row]->sub_level2 = $this->getPointSumaryLevel2($item->type, $user_id, $vip_id, $item->created_date,$item->redemption_id);
			}
			else{
				$rs[$row]->des =  JText::_('WRD_MYACCOUNT_UNKNOWN');
			}
		}
		
		return $rs;
	}
	
	
	function getPointSumaryLevel2($type,$user_id,$vip_id,$extend,$code = 0){
		$db = &JFactory::getDbo();
		
		if($type == 0){
			//get transaction detail
			$extend = date('Y-m-d',strtotime($extend));
			
			$query = "SELECT a.*,b.point,b.expire ,o.`name` as outlet_name FROM #__wrd_transaction as a
			LEFT JOIN #__wrd_point as b ON a.transaction_id = b.`transaction` 
			LEFT JOIN #__wrd_outlets as o On a.outlet = o.`code` 
			WHERE b.type = 0 AND a.date_receipt
			BETWEEN '".$extend." 00:00:00' AND '".$extend." 23:59:59' AND vip_number =". $vip_id." 
			ORDER BY created_date DESC";
			
			$tmp_rs = new stdClass();
			$tmp_rs->level2 = $db->setQuery($query)->loadObjectList();
			$total = 0;
			foreach ($tmp_rs->level2 as $rs){
				$total += intval($rs->point);
			}
			$tmp_rs->total = $total;
			
			return $tmp_rs;
		}
		
		if($type == 1){
			//get profile
			$extend = date('Y-m-d',strtotime($extend));
			$query = "SELECT *
			FROM #__wrd_point
			WHERE created_date BETWEEN '".$extend." 00:00:00' AND '".$extend." 23:59:59'
			AND type = 1
			AND user_id = ".$user_id."
			ORDER BY created_date DESC";
			
			$tmp_rs = new stdClass();
			$tmp_rs->level2 = $db->setQuery($query)->loadObjectList();
			$total = 0;
			
			foreach ($tmp_rs->level2 as $rs){
				$total += intval($rs->point);
			}
			$tmp_rs->total = $total;

			return $tmp_rs;
		}
		
		if($type == 2){
			$extend = date('Y-m-d',strtotime($extend));
			$query = "SELECT *
			FROM #__wrd_point
			WHERE created_date BETWEEN '".$extend." 00:00:00' AND '".$extend." 23:59:59'
			AND type = 2
			AND user_id = ".$user_id."
			ORDER BY created_date DESC";
			
			$tmp_rs = new stdClass();
			$tmp_rs->level2 = $db->setQuery($query)->loadObjectList();
			$total = 0;
			
			foreach ($tmp_rs->level2 as $rs){
				$total += intval($rs->point);
			}
			$tmp_rs->total = $total;

			return $tmp_rs;
		}
		
		if($type == 9){
			// get redemption detail
			$extend = date('Y-m-d',strtotime($extend));
			$query = "SELECT a.*,p.name as product_name
			FROM #__wrd_redemption as a
			LEFT JOIN #__wrd_products as p ON a.product_id = p.id
			WHERE
			a.exchange_date BETWEEN '{$extend} 00:00:00'
					AND '{$extend} 23:59:59'
							AND user_id = {$user_id}
							AND code = '{$code}'
							ORDER BY exchange_date DESC";
			//echo $query;
			$tmp_rs = new stdClass();
			$tmp_rs->level2 = $db->setQuery($query)->loadObjectList();
			$total = 0;
				
			foreach ($tmp_rs->level2 as $rs){
				$total += intval($rs->point);
			}
			$tmp_rs->total = $total;
			
			return $tmp_rs;
			
		}
	}
}
?>